﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SaleManagement.Controllers;
namespace SaleManagement.modules
{
    public partial class frmAdd_Edit_MatHang : Form
    {
        private usMatHang usMatHang;

        public frmAdd_Edit_MatHang(usMatHang usMatHang)
        {
            InitializeComponent();
            this.usMatHang = usMatHang;
            if (usMatHang.add == false)
            {
                txtCode.Text = usMatHang.DataGridView.Rows[usMatHang.row].Cells[1].Value.ToString();
                txtName.Text = usMatHang.DataGridView.Rows[usMatHang.row].Cells[2].Value.ToString();
                txtImport_price.Text = usMatHang.DataGridView.Rows[usMatHang.row].Cells[4].Value.ToString();
                txtWholesale_price.Text = usMatHang.DataGridView.Rows[usMatHang.row].Cells[5].Value.ToString();
                txtRetail_price.Text = usMatHang.DataGridView.Rows[usMatHang.row].Cells[6].Value.ToString();
                rtbDescription.Text = usMatHang.DataGridView.Rows[usMatHang.row].Cells[7].Value.ToString();
                dtpExpirate.Text = usMatHang.DataGridView.Rows[usMatHang.row].Cells[8].Value.ToString();
            }
            else
            {
                this.clear();
            }
        }



        private void clear()
        {
            txtName.Text = "";
            txtCode.Text = "";
            dtpExpirate.Text = "";
        }

        private void btlLuu_Click(object sender, EventArgs e)
        {
            GoodController good = new GoodController();
            string code = txtCode.Text;
            string name = txtName.Text;
            string expirate = dtpExpirate.Text;
            string import_price = txtImport_price.Text;
            string wholesale_price = txtWholesale_price.Text;
            string retail_price = txtRetail_price.Text;
            string ware = txtWare.Text;
            string unit = "1";
            string description = rtbDescription.Text;
            if (usMatHang.add == true)
            {
                if (good.insertGood(2,code,name,expirate,import_price,wholesale_price,retail_price,ware,description,unit) == true)
                {
                    label1.Text = good.mess;
                    usMatHang.onLoad();
                    this.clear();
                }
            }
            else
            {
                if (good.EditGood(usMatHang.idgood, code, name, expirate) == true)
                {
                    label1.Text = good.mess;
                    usMatHang.onLoad();
                    usMatHang.idgood = null;
                    this.Close();
                }
                labelControl1.Text = good.mess;
            }
        }
    }
}
